﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using System.Data;
using System.Data.SqlClient;

namespace LotteryVote.Data
{
    public class UserInfoDataAccess : DataBase
    {
        public void Insert(UserInfo userInfo)
        {
            CheckUserInfoValid(userInfo);
            string sql = string.Format(@"Insert into tb_UserInfo ({0},{1},{2},{3},{4},{5},{6},{7}) values(@{0},@{1},@{2},@{3},@{4},@{5},@{6},@{7})",
                UserInfo.USERID, UserInfo.PASSWORD, UserInfo.NAME, UserInfo.STATE, UserInfo.GIVENCREDIT, UserInfo.AVAILABLEGIVENCREDIT, UserInfo.SHARERATE, UserInfo.EMAIL);
            base.ExecuteNonQuery(sql,
            new SqlParameter(UserInfo.USERID, userInfo.UserId),
            new SqlParameter(UserInfo.PASSWORD, userInfo.Password),
            new SqlParameter(UserInfo.NAME, userInfo.Name),
            new SqlParameter(UserInfo.STATE, (int)userInfo.State),
            new SqlParameter(UserInfo.GIVENCREDIT, userInfo.GivenCredit),
            new SqlParameter(UserInfo.AVAILABLEGIVENCREDIT, userInfo.AvailableGivenCredit),
            new SqlParameter(UserInfo.SHARERATE, userInfo.ShareRate),
            new SqlParameter(UserInfo.EMAIL, userInfo.Email));
        }
        public void Update(UserInfo userInfo)
        {
            CheckUserInfoValid(userInfo);
            string sql = string.Format(@"UPDATE {0} SET {1}=@{1},{2}=@{2},{3}=@{3},{4}=@{4},{5}=@{5},{6}=@{6},{7}=@{7} WHERE {8}=@{8}",
                UserInfo.TABLENAME, UserInfo.PASSWORD, UserInfo.NAME, UserInfo.STATE,
                UserInfo.GIVENCREDIT, UserInfo.AVAILABLEGIVENCREDIT, UserInfo.SHARERATE, UserInfo.EMAIL, UserInfo.USERID);
            base.ExecuteNonQuery(sql,
                new SqlParameter(UserInfo.USERID, userInfo.UserId),
                new SqlParameter(UserInfo.PASSWORD, userInfo.Password),
                new SqlParameter(UserInfo.NAME, userInfo.Name),
                new SqlParameter(UserInfo.STATE, (int)userInfo.State),
                new SqlParameter(UserInfo.GIVENCREDIT, userInfo.GivenCredit),
                new SqlParameter(UserInfo.AVAILABLEGIVENCREDIT, userInfo.AvailableGivenCredit),
                new SqlParameter(UserInfo.SHARERATE, userInfo.ShareRate),
                new SqlParameter(UserInfo.EMAIL, userInfo.Email));
        }

        public UserInfo GetUserInfo(int userId)
        {
            string sql = string.Format(@"SELECT * FROM {0} WHERE {1}=@{1}", UserInfo.TABLENAME, UserInfo.USERID);
            return base.ExecuteEntity<UserInfo>(sql, new SqlParameter(UserInfo.USERID, userId));
        }

        private void CheckUserInfoValid(UserInfo userInfo)
        {
            if (userInfo == null)
                throw new ArgumentException("UserInfo is null!");
            if (userInfo.UserId <= 0)
                throw new ArgumentException("UserId must great than 0!");
            if (string.IsNullOrEmpty(userInfo.Password))
                throw new ArgumentException("User's password is empty!");
            if (string.IsNullOrEmpty(userInfo.Name))
                throw new ArgumentException("User's name is empty!");
        }
    }
}
